package com.wxy.demo.util;

import cn.hutool.http.ContentType;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import com.wxy.demo.core.ApiResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/***
 * <p>
 * Description: 向客户端写入数据
 * </p>
 * @author wangxiaoyuan
 * 2021年12月09日
 */
public class ResponseUtils {
    public static <T> void write(HttpServletResponse response, ApiResponse<T> body) throws IOException {
        response.setStatus(HttpStatus.HTTP_OK);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(ContentType.JSON.getValue());
        response.getWriter().write(JSONUtil.toJsonStr(body));
    }
}
